<template>
    <div> 
         <Layout>
      <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
        <FormItem label="用户名" prop="userName" class="Position">
            <Input v-model="formValidate.userName" placeholder="输入你的用户名"></Input>
        </FormItem>
         <FormItem label="密码" prop="userpwd">
            <Input v-model="formValidate.userpwd" placeholder="输入你的密码"></Input>
        </FormItem>
         <FormItem label="学生号" prop="studentnum">
            <Input v-model="formValidate.studentnum" type="number"  placeholder="你的学生号"></Input>
        </FormItem>
        <FormItem label="E-mail" prop="mail">
              <AutoComplete
        v-model="formValidate.mail"
        @on-search="handleSearch2"
        placeholder="输入你的Email"
        style="width:100%">
        <Option v-for="item in data2" :value="item" :key="item">{{ item }}</Option>
              </AutoComplete>
        </FormItem>
               <FormItem label="电话" prop="mobile">
            <Input  type="tel"  v-model="formValidate.mobile" placeholder="输入你的电话"></Input>
        </FormItem>
        <FormItem label="年级" prop="grade">
            <Input v-model="formValidate.grade" placeholder="输入你的年级">
            </Input>
        </FormItem>
        <FormItem label="出生日期">
            <Row>
                <Col span="11">
                    <FormItem prop="date">
                        <DatePicker size="large" v-model="formValidate.date"  :key="formValidate.date" format="yyyy-MM-dd" @on-change="formValidate.date=$event" type="date" placeholder="Select date" style="width: 200px"></DatePicker>
                    </FormItem>
                </Col>
            </Row>
        </FormItem>
        <FormItem label="性别" prop="gender">
            <RadioGroup v-model="formValidate.gender">
                <Radio label="男">男</Radio>
                <Radio label="女">女</Radio>
            </RadioGroup>
        </FormItem>
        <FormItem label="专业" prop="major">
            <Select v-model="formValidate.major">
                <Option value="通信工程">通信工程</Option>
                <Option value="软件工程">软件工程</Option>
                <Option value="计算机科学">计算机科学</Option>
                <Option value="大数据分析">大数据分析</Option>
            </Select>
        </FormItem>
        <FormItem>
            <Button type="primary" @click.once="handleSubmit('formValidate')">提交</Button>
            <Button @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
        </FormItem>
    </Form>
     </Layout>
  </div>
</template>
<script>
const validatePhone = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('手机号不能为空'));
            } else if (!/^1[34578]\d{9}$/.test(value)) {
                callback('手机号格式不正确');
            } else {
                callback();
            }   
        };
    export default {
        data () {
            return {
                data2: [],
                formValidate: {
                    userName: '',
                    userpwd:'',
                    studentnum:'',
                    mobile:'',
                    mail: '',
                    grade: '',
                    gender: '',
                    major: '',
                    date: '',
                },
                ruleValidate: {
                    userName: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                      userpwd: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        {min:6,max:20,message:'密码长度为6到20位',trigger:'change'},
                        {pattern:/^[A-Z-z0-9]+$/,message:'只能是数字或字母'}

                    ],
                    mail: [
                        { required: true, message: '邮箱不能为空', trigger: 'blur' },
                    ],
                    mobile: [
                          { required: true,validator:validatePhone,trigger:'blur'}
                    ],
                    grade: [
                        { required: true, message: '请选择你的年级', trigger: 'blur' }
                    ],
                    gender: [
                        { required: true, message: '请选择你的性别', trigger: 'change' }
                    ],
                    major: [
                         { required: true, message: '请选择你的专业', trigger: 'change' }    
                    ],
                    date: [
                        { required: true, type: 'date', message: '请选择你的出生日期', trigger: 'change' }
                    ],
                     studentnum: [
                        { required: true, message: '请输入你的学生号', trigger: 'blur' },
                          {len:10,message:'学生号长度为10位',trigger:'change'},
                         {pattern:/^[A-Z-z0-9]+$/,message:'只能是数字或字母'}
                    ],
                }
            }
        },
        methods: {
            handleSubmit (name) {
                var userNameMsg={userName:this.formValidate.userName}
                this.$refs[name].validate((valid) => {
                    if (valid) {
                   this.$axios({
                   method:"post",
                   url:"http://"+this.urlpath+"/FindOne",
                   data:userNameMsg
               }).then((res)=>{
               if(res.data.userName!==this.formValidate.userName){
                   this.$axios({                                                                                
                   method:"post",
                   url:"http://"+this.urlpath+"/CreateNewUser",
                   data:this.formValidate
               }).then((res)=>{
                   
               })
                  this.$Message.success('提交成功!');
                   }
                else if(res.data.userName===this.formValidate.userName){
                       this.$Message.error('用户名已存在!')
                   } 
                else{this.$Message.error('用户名已存在!')
                   }                  
               })
                    } else {
                        this.$Message.error('内容有误!');
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            },
             handleSearch2 (value) {
                this.data2 = !value || value.indexOf('@') >= 0 ? [] : [
                    value + '@qq.com',
                    value + '@sina.com',
                    value + '@163.com'
                ];
            },
        }
    }
</script>
<style scoped>
  .Position{
    margin-top:30px; 
  }
</style>
